package 算法.综合练习;

import java.util.Arrays;
import java.util.Comparator;

public class test1 {
    /*
    定义数组并存储一些女朋友对象，利用Arrays中的sort方法进行排序
    要求1：属性有姓名、年龄、身高（为girl写一个类存储属性）
    要求2：按照年龄大小进行排序，年龄一样，按照身高排序，身高一样，按照姓名的字母进行排序
    （姓名中不要有中文或特殊字符）  以上可以通过三元运算符来运算
    即     年龄差==0？身高差：temp
     */
    public static void main(String[] args) {

/*
//            compareTo();专用于字符串的比较
        String S1 = "a";
        String S2 = "b";
        int i = S1.compareTo(S2);
        System.out.println(i);//-1单个字符会通过Ascll码表比较大小
        String s1="asd";
        String s2="asc";
        int i1=s1.compareTo(s2);
        System.out.println(i1);//-1 多个字符会遍历字符串一个一个进行比较
 */
        GirlFriend girl1=new GirlFriend("xiaoshishi",18,1.67);
        GirlFriend girl2=new GirlFriend("xiaodandan",19,1.67);
        GirlFriend girl3=new GirlFriend("xiaohuihui",18,1.72);
        GirlFriend girl4=new GirlFriend("qiaoyijun",18,1.67);
        GirlFriend [] arr={girl1,girl2,girl3,girl4};

        Arrays.sort(arr, new Comparator<GirlFriend>() {
            @Override
            public int compare(GirlFriend o1, GirlFriend o2) {
                double temp= o1.getAge()- o2.getAge();//先判断年龄大小
                temp=temp==0?o1.getHeight()-o2.getHeight():temp;
                temp=temp==0?o1.getName().compareTo(o2.getName()):temp;
                if (temp>0){
                    return 1;
                } else if (temp<0) {
                    return -1;
                }else
                {
                    return 0;
                }
            }
        });
        /*
//        lambda表达式
        Arrays.sort(arr, (GirlFriend o1, GirlFriend o2)->{
                double temp= o1.getAge()- o2.getAge();//先判断年龄大小
                temp=temp==0?o1.getHeight()-o2.getHeight():temp;
                temp=temp==0?o1.getName().compareTo(o2.getName()):temp;
                if (temp>0){
                    return 1;
                } else if (temp<0) {
                    return -1;
                }else
                {
                    return 0;
                }
        });
         */

//        展示数组中的比较
        System.out.println(Arrays.toString(arr));


    }
}
class GirlFriend{
    private String name;
    private int age;
    private double height;

    public GirlFriend() {
    }

    public GirlFriend(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return height
     */
    public double getHeight() {
        return height;
    }

    /**
     * 设置
     * @param height
     */
    public void setHeight(double height) {
        this.height = height;
    }

    public String toString() {
        return "GirlFriend{name = " + name + ", age = " + age + ", height = " + height + "}";
    }
}